<?php
class Paginator {
  
  var $pages;
  var $page_subjects;
  var $linkable;
  var $current_page;
  var $link;
  
  function __construct($page_subjects, $linkable, $current_page, $rows, $redirect_page, $params) {
    $this->page_subjects = $page_subjects;
    $this->current_page = $current_page;
    $this->linkable = $linkable;
    
    if (!isset($current_page)) $this->current_page = 0;
    
    $this->pages = ceil($rows / $page_subjects);        // počet listů s články
    if($this->pages == 0) $this->pages = 1;
    
    $this->link = REDIRECT.$redirect_page.$params.add_par("current=");
  }
  
  function getFirstPageIndex(){
    return $this->current_page * $this->page_subjects;      // od kterého článku vypisovat nadpisy
  }
  
  function getPageSubjects(){
    return $this->page_subjects;
  }
  
  function show(){
      echo "<div class='paginator center'><table><tr>";
      if ($this->current_page > 0) {
        echo "<td><a class='preloader' href=\"".$this->link.($this->current_page-1)."\"><<</a></td>";
        echo "<td><a class='preloader' href=\"".$this->link."0\">1</a></td>";
      } else {
         echo "<td class='disabled'><<</td>";
         echo "<td class='current'>1</td>";
      } 
    
      if($this->current_page > $this->linkable + 1) {
        echo "<td class='hole'>..</td>"; 
      }
    
      $start = $this->current_page - $this->linkable;
      if($start < 1) $start = 1;
      for($i = $start; $i < $this->current_page; $i++) {
          echo "<td><a class='preloader' href=\"".$this->link."$i\">".($i+1)."</a></td>";
      }
    
      if($this->current_page > 0 && $this->current_page < $this->pages - 1) echo "<td class='current'>".($this->current_page+1)."</td>";
    
      $finish = $this->current_page + $this->linkable;
      if($finish >= $this->pages - 1) $finish = $this->pages - 2;
      
      for($i = $this->current_page + 1; $i <= $finish; $i++) {
        echo "<td><a class='preloader' href=\"".$this->link."$i\">".($i+1)."</a></td>";
      }
    
      if($this->current_page < $this->pages - $this->linkable - 2) {  
        echo "<td class='hole'>..</td>";
      }
    
      if ($this->current_page < $this->pages - 1) {
        echo "<td><a class='preloader' href=\"".$this->link.($this->pages-1)."\">".$this->pages."</a></td>";
        echo "<td><a class='preloader' href=\"".$this->link.($this->current_page+1)."\">>></a></td>";
      }else {
        if($this->pages > 1){
          echo "<td class='current'>$this->pages</td>";
        }
        echo "<td class='disabled'>>></td>";
      }
      echo "</tr></table></div>";
  }
}
?>
